package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;

import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.sales.domain.NcContract;
import com.lxmes.sales.domain.NcOrderResourcesYs;
import com.lxmes.sales.domain.NcProcessproPlan;
import com.lxmes.sales.mapper.NcOrderResourcesYsMapper;
import com.lxmes.sales.mapper.NcProcessproPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.sales.service.INcProcessproPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 加工排产计划Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Service
public class NcProcessproPlanServiceImpl implements INcProcessproPlanService
{
    @Autowired
    private NcProcessproPlanMapper ncProcessproPlanMapper;

    @Autowired
    private NcOrderResourcesYsMapper ncOrderResourcesYsMapper;

    /**
     * 查询加工排产计划
     *
     * @param id 加工排产计划主键
     * @return 加工排产计划
     */
    @Override
    public NcProcessproPlan selectNcProcessproPlanById(Long id)
    {
        return ncProcessproPlanMapper.selectNcProcessproPlanById(id);
    }

    /**
     * 查询加工排产计划列表
     *
     * @param ncProcessproPlan 加工排产计划
     * @return 加工排产计划
     */
    @Override
    public List<NcProcessproPlan> selectNcProcessproPlanList(NcProcessproPlan ncProcessproPlan)
    {
        List<NcProcessproPlan> ncProcessproPlans = ncProcessproPlanMapper.selectNcProcessproPlanList(ncProcessproPlan);
        for(NcProcessproPlan plan : ncProcessproPlans){
            NcContract ncContract = ncProcessproPlanMapper.getClientContractNo(plan.getContractNo());
            if(StringUtils.isNotNull(ncContract)){
                plan.setClientContractNo(ncContract.getClientContractNo());
            }
        }
        return ncProcessproPlanMapper.selectNcProcessproPlanList(ncProcessproPlan);
    }

    /**
     * 新增加工排产计划
     *
     * @param ncProcessproPlan 加工排产计划
     * @return 结果
     */
    @Override
    public int insertNcProcessproPlan(NcProcessproPlan ncProcessproPlan)
    {
        return ncProcessproPlanMapper.insertNcProcessproPlan(ncProcessproPlan);
    }

    /**
     * 修改加工排产计划
     *
     * @param ncProcessproPlan 加工排产计划
     * @return 结果
     */
    @Override
    public int updateNcProcessproPlan(NcProcessproPlan ncProcessproPlan)
    {
        ncProcessproPlan.setUpdateBy(SecurityUtils.getUsername());
        ncProcessproPlan.setUpdateTime(new Date());
        return ncProcessproPlanMapper.updateNcProcessproPlan(ncProcessproPlan);
    }

    /**
     * 批量删除加工排产计划
     *
     * @param ids 需要删除的加工排产计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNcProcessproPlanByIds(Long[] ids)
    {
        for(long id : ids){
            NcProcessproPlan ncProcessproPlan = ncProcessproPlanMapper.selectNcProcessproPlanById(id);
            if("0".equals(ncProcessproPlan.getIsRead())){
                ncProcessproPlanMapper.deleteNcProcessproPlanById(id);
                NcProcessproPlan plan = new NcProcessproPlan();
                plan.setItemIdentifyCode(ncProcessproPlan.getItemIdentifyCode());
                plan.setOrderNo(ncProcessproPlan.getOrderNo());
                List<NcProcessproPlan> ncplans = ncProcessproPlanMapper.selectNcProcessproPlan(plan);
                if(ncplans.size()==0){
                    //解销售封印
                    ncProcessproPlanMapper.updateSalesState("0",ncProcessproPlan.getItemIdentifyCode(),ncProcessproPlan.getOrderNo());
                }
            }else{
                throw new RuntimeException("排产已经被读取不能删除！");
            }
        }
        return 1;
    }

    /**
     * 删除加工排产计划信息
     *
     * @param id 加工排产计划主键
     * @return 结果
     */
    @Override
    public int deleteNcProcessproPlanById(Long id)
    {
        return ncProcessproPlanMapper.deleteNcProcessproPlanById(id);
    }

    @Override
    public void handleRead() {
        List<NcOrderResourcesYs> ncOrderResourcesYs = ncOrderResourcesYsMapper.selectYsList();
        for(NcOrderResourcesYs ys : ncOrderResourcesYs){
            NcProcessproPlan ncProcessproPlan = new NcProcessproPlan();
            ncProcessproPlan.setOrderNo(ys.getOrderNo());
            List<NcProcessproPlan> ncProcessproPlans = ncProcessproPlanMapper.selectNcProcessproPlanList(ncProcessproPlan);
            if(ncProcessproPlans.size()==0){
                ncProcessproPlan.setFactoryCode("WR");
                ncProcessproPlan.setFactoryName("玮瑞");

                ncProcessproPlan.setContractNo(ys.getHth());
                String pcCode= "TKPC-"+ NewNumber.createNewNumber(ncProcessproPlanMapper.getNewCode(DateUtils.getDate()));
                ncProcessproPlan.setPcCode(pcCode);
                ncProcessproPlan.setPcDate(new Date());
                ncProcessproPlan.setJhDate(ys.getDeliveryDate());
                ncProcessproPlan.setPcNum(ys.getPcNum());
                ncProcessproPlanMapper.insertNcProcessproPlan(ncProcessproPlan);
            }
        }
    }
}
